/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.own.service.OwnMedicineReceiptInformationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnMedicineInspectionInformation;
import com.jeesite.modules.own.service.OwnMedicineInspectionInformationService;

import java.util.Map;

/**
 * 药材检验信息Controller
 * @author F
 * @version 2019-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownMedicineInspectionInformation")
public class OwnMedicineInspectionInformationController extends BaseController {

	@Autowired
	private OwnMedicineInspectionInformationService ownMedicineInspectionInformationService;

	@Autowired
	private OwnMedicineReceiptInformationService ownMedicineReceiptInformationService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnMedicineInspectionInformation get(String id, boolean isNewRecord) {
		return ownMedicineInspectionInformationService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnMedicineInspectionInformation ownMedicineInspectionInformation, Model model) {
		model.addAttribute("ownMedicineInspectionInformation", ownMedicineInspectionInformation);
		return "modules/own/ownMedicineInspectionInformationList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnMedicineInspectionInformation> listData(OwnMedicineInspectionInformation ownMedicineInspectionInformation, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineInspectionInformation.setPage(new Page<>(request, response));
		Page<OwnMedicineInspectionInformation> page = ownMedicineInspectionInformationService.findPage(ownMedicineInspectionInformation);
		return page;
	}
	/**
	 * 查询还在种植中的列表数据
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:view")
	@RequestMapping(value = "listDataNormal")
	@ResponseBody
	public Page<OwnMedicineInspectionInformation> listDataNormal(OwnMedicineInspectionInformation ownMedicineInspectionInformation, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineInspectionInformation.setPage(new Page<>(request, response));
		String[] strs = {"一级","二级", "三级"};
		ownMedicineInspectionInformation.setCheckGrade_in(strs);
		Page<OwnMedicineInspectionInformation> page = ownMedicineInspectionInformationService.findPage(ownMedicineInspectionInformation);
		return page;
	}


	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:view")
	@RequestMapping(value = "form")
	public String form(OwnMedicineInspectionInformation ownMedicineInspectionInformation, Model model) {
		model.addAttribute("ownMedicineInspectionInformation", ownMedicineInspectionInformation);
		return "modules/own/ownMedicineInspectionInformationForm";
	}

	/**
	 * 保存药材检验信息
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnMedicineInspectionInformation ownMedicineInspectionInformation) {
		ownMedicineInspectionInformationService.save(ownMedicineInspectionInformation);
		if (ownMedicineInspectionInformation.getIsNewRecord())
			ownMedicineReceiptInformationService.updateInspectionSituation(ownMedicineInspectionInformation.getOwnMedicineReceiptInformationId(), "已检验");
		return renderResult(Global.TRUE, text("保存药材检验信息成功！"));
	}
	
	/**
	 * 删除药材检验信息
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnMedicineInspectionInformation ownMedicineInspectionInformation) {
		ownMedicineInspectionInformationService.delete(ownMedicineInspectionInformation);
		return renderResult(Global.TRUE, text("删除药材检验信息成功！"));
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:view")
	@RequestMapping(value = "findByKey")
	public String plantInfoSelect(OwnMedicineInspectionInformation ownMedicinePlantInformation, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (JsonMapper.fromJson(selectDataJson, Map.class) != null){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("inspectionInformation", ownMedicinePlantInformation);
		return "modules/own/ownMedicineInspectionInformationSelect";
	}

	/**
	 * 根据收获id查询数据
	 */
	@RequiresPermissions("own:ownMedicineInspectionInformation:view")
	@RequestMapping(value = "findByReceiptId")
	@ResponseBody
	public String findByReceiptId(String receiptId) {
		return renderResult(Global.TRUE, text("查询药材检验信息成功！"), ownMedicineInspectionInformationService.findByReceiptId(receiptId));
	}
	
}